package com.greenit.phoneinfo;

import android.app.Activity;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import android.telephony.SignalStrength;

public class phone_info extends Activity {
	static final String tag = "Main"; // for Log

    private Button closeButton;
    private Button gpsinfoButton;
    private Button cellsigButton;
    private TextView updateText;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    	LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //Check GPS status       
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            Toast.makeText(this, "GPS is Enabled on your device", Toast.LENGTH_SHORT).show();
        }else{
        	Toast.makeText(this, "Reminder: Your GPS is not enabled.", Toast.LENGTH_SHORT).show();      	
        }
                       
        //Get GPS info Button        
        gpsinfoButton = (Button)this.findViewById(R.id.getgpsinfo);
        gpsinfoButton.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
                dispgpsinfo();
        	} 
        });
        //Get Cell Signal Button
        cellsigButton = (Button)this.findViewById(R.id.getcellsig);
        cellsigButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
        		dispcellinfo();         	
          }		
        }); 
        
        //Exit App Button
        closeButton = (Button)this.findViewById(R.id.exit);
        closeButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
        		finish();          	
          }		
        });  
        
        //GPS status
        final CheckBox checkBox = (CheckBox) findViewById(R.id.GPSstate);
        checkBox.setOnClickListener(new OnClickListener(){
        	public void onClick(View v) {
        		if(checkBox.isChecked()){
        		    Context context = getApplicationContext();
    			    CharSequence text = "GPS Enabled!";
    			    int duration = Toast.LENGTH_LONG;
         			Toast toast = Toast.makeText(context, text, duration);
        			toast.show();
        		}else
        		{
        		    Context context = getApplicationContext();
    			    CharSequence text = "GPS Disabled!";
    			    int duration = Toast.LENGTH_LONG;
         			Toast toast = Toast.makeText(context, text, duration);
        			toast.show();

        		}
        	}
        });
    }  
    
    //Cell Signal Information
    private void dispcellinfo(){
    	TelephonyManager telephonyInfo;
    	telephonyInfo = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    	
    	PhoneStateListener phoneState = new PhoneStateListener(){
    		public void onSignalStrengthsChanged(SignalStrength signalStrength){
    			String signal = null;
    			if(signalStrength.isGsm()){
    				//GSM
    			}else{
					if (signalStrength.getCdmaDbm() < 0){
    					signal = "CDMA";
    				}else if (signalStrength.getEvdoDbm() < 0){
    					signal = "EVDO";
    				}	
    			}
    			int cdma_signal = signalStrength.getCdmaDbm();
    			cdma_signal = 113+cdma_signal/2;
    			int evdo_signal = signalStrength.getEvdoDbm();
    			evdo_signal = 113+evdo_signal/2;
    		
    			super.onSignalStrengthsChanged(signalStrength);
    	         Toast.makeText(getApplicationContext(), signal + "= "
    	            + cdma_signal + "Db\nEVDO " + evdo_signal + "Db" , Toast.LENGTH_LONG).show();
    		}
    	};
    	
    	telephonyInfo.listen(phoneState, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
    	 	
		//updateText = (TextView)findViewById(R.id.output);
		
    }

    private void dispgpsinfo(){
		updateText = (TextView)findViewById(R.id.output);
    	// Acquire a reference to the system Location Manager
    	LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    	
    	// Define a listener that responds to location updates
    	LocationListener locationListener = new LocationListener() {
    	    public void onLocationChanged(Location location) {
    	      // Called when a new location is found by the network location provider.
    	    	int noOfFixes = 0;
    	  
				//makeUseOfNewLocation(location);
    			Log.v(tag, "Location Changed");

    			StringBuilder sb = new StringBuilder(512);

				noOfFixes++;

    			/* display some of the data in the TextView */

    			sb.append("No. of Fixes: ");
    			sb.append(noOfFixes);
    			sb.append('\n');
    			sb.append('\n');

    			sb.append("Londitude: ");
    			sb.append(location.getLongitude());
    			sb.append('\n');

    			sb.append("Latitude: ");
    			sb.append(location.getLatitude());
    			sb.append('\n');

    			sb.append("Provider: ");
    			sb.append(location.getProvider());
    			sb.append('\n');            			

    			sb.append("Altitiude: ");
    			sb.append(location.getAltitude());
    			sb.append('\n');

    			sb.append("Accuracy: ");
    			sb.append(location.getAccuracy());
    			sb.append('\n');

    			sb.append("Timestamp: ");
    			sb.append(location.getTime());
    			sb.append('\n');

    			updateText.setText(sb.toString());          	      
    	    }

			@Override
			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub	
				Context context = getApplicationContext();
				CharSequence text = "Reminder your GPS is Disabled!";
				int duration = Toast.LENGTH_LONG;
				Toast toast = Toast.makeText(context, text, duration);
				toast.show();	
				checkGps();
			}

			@Override
			public void onProviderEnabled(String provider) {
				// TODO Auto-generated method stub
				Context context = getApplicationContext();
				CharSequence text = "GPS Enabled!";
				int duration = Toast.LENGTH_LONG;
				Toast toast = Toast.makeText(context, text, duration);
				toast.show();
				enableGPS();
			}

			@Override
			public void onStatusChanged(String provider, int status,
					Bundle extras) {
				// TODO Auto-generated method stub				
			}
    	};
		// Register the listener with the Location Manager to receive location updates
    	locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }
    //Disable GPS
    private void disableGPS(){
    	
    }
    //Enable GPS
    private void enableGPS(){
    	startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
    }
    
    public void checkGps(){
		final Dialog dialog=new Dialog(this);
    	dialog.setContentView(R.layout.gps);
    	dialog.setTitle("GPS");
    	dialog.setCancelable(false);
    	dialog.show();
    	
    	Button gpsSettings = (Button) dialog.findViewById(R.id.gps_set_button);
    	gpsSettings.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
			}
		});
		
    	Button gpsCancel = (Button) dialog.findViewById(R.id.gps_cancel_button);
    	gpsCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
	}
  }
